#include "utils.h"
#include <memory.h>

void ConvertFftwToComplex(uint32_t width,uint32_t height,const float* ptrPacked, float* ptrComplexDest, uint32_t destStride)
{
	uint32_t destHeight = height;
	uint32_t destWidth = width / 2 + 1;

	uint32_t rem = width - destWidth;
	for (int y = 0; y < destHeight; ++y)
	{
		float* ptrDest = (float*)(((char*)ptrComplexDest) + y*destStride);
		const float* ptrSrc = (const float*)(((char*)ptrPacked) + y*(destWidth * 2 * sizeof(float)));
		if (y == 0)
		{
			memcpy(ptrDest, ptrSrc, destWidth*sizeof(float) * 2);
			for (int i = 0; i < rem; i++)
			{
				ptrDest[((width - 1) - i) * 2] = ptrSrc[(1 + i) * 2];
				ptrDest[((width - 1) - i) * 2 + 1] = -ptrSrc[(1 + i) * 2 + 1];
			}
		}
		else
		{
			memcpy(ptrDest, ptrSrc, destWidth*sizeof(float) * 2);
			for (int i = 0; i < rem; i++)
			{
				uint32_t x_ = destWidth + i;
				uint32_t y_ = (y);

				uint32_t x_r = width - x_;
				uint32_t y_r = height - y_;

				ptrDest[x_ * 2] = ((const float*)(((char*)ptrPacked) + y_r*(destWidth * 2 * sizeof(float))))[x_r * 2];
				ptrDest[x_ * 2 + 1] = -((const float*)(((char*)ptrPacked) + y_r*(destWidth * 2 * sizeof(float))))[x_r * 2 + 1];
			}
		}
	}
}

double CStopWatch::LIToSecs(LARGE_INTEGER & L)
{
	return ((double)L.QuadPart / (double)this->frequency.QuadPart);
}

CStopWatch::CStopWatch()
{
	this->start.QuadPart = 0;
	this->stop.QuadPart = 0;
	QueryPerformanceFrequency(&this->frequency);
}

void CStopWatch::startTimer()
{
	QueryPerformanceCounter(&this->start);
}

void CStopWatch::stopTimer()
{
	QueryPerformanceCounter(&stop);
}

double CStopWatch::getElapsedTime()
{
	LARGE_INTEGER time;
	time.QuadPart = this->stop.QuadPart - this->start.QuadPart;
	return LIToSecs(time);
}
